﻿Imports ACompany.OrderProcessing.Model
Imports ACompany.OrderProcessing.Infrastructure
Imports ACompany.OrderProcessing.Services
Imports System.Text
Imports StructureMap

Public Class CustomerPresenter

    Private view As ICustomerView
    Private customerService As ICustomerService
    Private selectedCustomer As Customer
    Private mode As Mode

    Public Sub New(ByVal view As ICustomerView)
        Me.New(ObjectFactory.GetInstance(Of ICustomerService)(), view)
    End Sub

    Public Sub New(ByVal customerService As ICustomerService, ByVal view As ICustomerView)
        Me.view = view
        Me.customerService = customerService
    End Sub

    Public Sub Display()

        Dim Customer As Customer = GetSelectedCustomerDetail()

        clearInfoAndErrorMessages()
        setButtonText()
        setTitleText()

        If Customer Is Nothing Then
            view.ErrorMessage(String.Format("Could not find details from Customer with Id of {0}.", view.CustomerId))
        Else
            UpdateView(Customer)
        End If

    End Sub

    Private Sub setButtonText()
        If mode = mode.Add Then
            view.SaveButtonText = "Add Customer"
        Else
            view.SaveButtonText = "Save Customer"
        End If
    End Sub

    Private Sub setTitleText()
        If mode = mode.Add Then
            view.ViewTitle("Add Customer")
        Else
            view.ViewTitle("Edit Customer")            
        End If
    End Sub

    Private Sub clearInfoAndErrorMessages()
        view.ErrorMessage(String.Empty)
        view.InfoMessage(String.Empty)
    End Sub

    Private Function GetSelectedCustomerDetail() As Customer

        If selectedCustomer Is Nothing Then
            If view.CustomerId = Guid.Empty Then
                ' Add mode
                selectedCustomer = New Customer
                mode = Presentation.Mode.Add
            Else
                ' Edit mode
                selectedCustomer = Me.customerService.FindBy(Me.view.CustomerId)
                mode = Presentation.Mode.Edit
            End If
        End If

        Return selectedCustomer

    End Function

    Private Sub UpdateView(ByVal selectedCustomer As Customer)

        view.Name = selectedCustomer.Name
        view.Email = selectedCustomer.Email

        If Not selectedCustomer.Address Is Nothing Then
            view.Street = selectedCustomer.Address.Street
            view.City = selectedCustomer.Address.City
            view.County = selectedCustomer.Address.County
            view.PostCode = selectedCustomer.Address.PostCode
        Else
            view.Street = String.Empty
            view.City = String.Empty
            view.County = String.Empty
            view.PostCode = String.Empty
        End If

    End Sub

    Public Sub SaveCustomer()

        Dim Customer As Customer = GetSelectedCustomerDetail()
        clearInfoAndErrorMessages()

        Try
            UpdateFromView(Customer)

            If Customer.GetBrokenRules.Count = 0 Then
                Try
                    Me.customerService.Save(Customer)

                    If mode = mode.Add Then
                        Me.view.InfoMessage(String.Format("Customer '{0}' has been added", Customer.Name))
                        'NavigationService.NavigateTo("customers")
                        ClearView()
                    Else
                        Me.view.InfoMessage("Customer has been saved")
                    End If

                Catch ex As Exception

                    Dim exceptionMessage As String = String.Format("A problem occurred while saving customer '{0}'. {1}{2}", Customer.Name, Environment.NewLine, ex.Message)
                    view.ErrorMessage(exceptionMessage)

                End Try
            Else
                DisplayErrorsIn(Customer)
            End If

        Catch ex As ValueObjectValidationException
            view.ErrorMessage(ex.Message)
        End Try

    End Sub

    Private Sub UpdateFromView(ByVal Customer As Customer)
        Customer.Name = view.Name
        Customer.Email = view.Email

        Dim customerAddress As Address

        customerAddress = New Address(view.Street, view.City, view.County, view.PostCode)
        Customer.Address = customerAddress
    End Sub

    Private Sub ClearView()
        view.Name = String.Empty
        view.Email = String.Empty

        view.Street = String.Empty
        view.City = String.Empty
        view.County = String.Empty
        view.PostCode = String.Empty
    End Sub

    Private Sub DisplayErrorsIn(ByVal Customer As Customer)

        Dim sb As New StringBuilder

        sb.Append("There were some problems when trying to save this Customer:")
        sb.Append(Environment.NewLine)
        sb.Append(Environment.NewLine)

        For Each BrokenRule As BrokenRule In Customer.GetBrokenRules
            sb.Append(BrokenRule.Rule)
            sb.Append(Environment.NewLine)
        Next

        view.ErrorMessage(sb.ToString)
    End Sub

End Class
